const _ = require('../../utils/underscore')
Page({
  data: {
    result: [],
    count: 0
  },
  onLoad() {
    this.loadData()
  },
  onPullDownRefresh: function () {
    this.loadData()
  },
  loadData() {
    wx.request({
      url: `https://ig185.com/kaijiangweb/getDoubleHistory.do?date=&lotCode=GD115`,
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      success: (res) => {
        wx.stopPullDownRefresh()
        let result = res.data.data
        this.setData({
          result: this.format(result),
          count: this.count(result)
        })
      },
      fail: function (err) {
        console.log(err)
      }
    })
  },
  format(result) {
    result.forEach(item => {
      if (item.date === '2019-12-31' || item.date === '2019-12-30') {
        item.date = item.date.replace(/2019/, '2018')
      }
    })
    result = _.sortBy(result, 'date')
    result.reverse()
    return result
  },
  count(result) {
    let single = result.map(item => {
      return item.firstSingleCount
    })
    let double = result.map(item => {
      return item.firstDoubleCount
    })
    let singleCount = single.reduce((first, second) => {
      return first + second
    })
    let doubleCount = double.reduce((first, second) => {
      return first + second
    })
    return singleCount - doubleCount
  }
})